package com.mybatisx.mybatisplus.config;

import com.mybatisx.mybatisplus.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


/**
 * @author wuyue
 * @date 2020/1/3 15:51
 */

 @Configuration
public class MvcConfig implements WebMvcConfigurer {

    //服务器支持跨域
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST","OPTIONS")
                .allowedHeaders("*")
                .exposedHeaders("Access-Control-Allow-Headers",
                        "Access-Control-Allow-Methods",
                        "Access-Control-Allow-Origin",
                        "Access-Control-Max-Age",
                        "X-Frame-Options")
                .allowCredentials(false)
                .maxAge(3600);
    }





     /*将拦截器添加到spring容器*/
     @Bean
     public AuthenticationInterceptor authenticationInterceptor(){
         return  new AuthenticationInterceptor();
     }

      @Override
     /*将拦截器添加到拦截链中*/
      public void addInterceptors(InterceptorRegistry registry){
            //registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
          registry.addInterceptor(authenticationInterceptor()).addPathPatterns("/**");
      }
}
